@extends('base')

@section('content')
    <div class="layui-card">
        <div class="layui-card-header layuiadmin-card-header-auto">
            <form class="layui-form" action="{{route("caiwu.order")}}">
                <div class="layui-btn-group">
                    @can('caiwu.order.destroy')
                        <button type="button" class="layui-btn layui-btn-sm layui-btn-danger" data-url="{{route('caiwu.order.destroy')}}" id="listDelete">删除</button>
                    @endcan
                    @can('caiwu.order.create')
                        <button type="button" class="layui-btn layui-btn-sm" id="addBtn">添加</button>
                    @endcan
                    <button type="button" lay-submit lay-filter="search" class="layui-btn layui-btn-sm" >搜索</button>
                </div>
                <div class="layui-form-item">
                    <div class="layui-inline">
                        <label for="" class="layui-form-label">订单号：</label>
                        <div class="layui-input-block" style="width: 275px">
                            <input type="text" name="num" placeholder="请输入" class="layui-input" >
                        </div>
                    </div>
                </div>
            </form>
        </div>
        <div class="layui-card-body">
            <table id="dataTable" lay-filter="dataTable"></table>
            <script type="text/html" id="options">
                <div class="layui-btn-group">
                    <a class="layui-btn layui-btn-sm" lay-event="more">更多<i class="layui-icon layui-icon-down" style="font-size: 12px"></i></a>
                </div>
            </script>
        </div>
    </div>
@endsection

@section('script')
    <script>
        layui.use(['layer','table','form','laydate','upload','dropdown'],function () {
            var $ = layui.jquery;
            var layer = layui.layer;
            var form = layui.form;
            var table = layui.table;
            var laydate = layui.laydate;
            var upload = layui.upload;
            var dropdown = layui.dropdown
            //用户表格初始化
            var dataTable = table.render({
                elem: '#dataTable'
                ,height: 'full-200'
                ,url: "{{ route('caiwu.order') }}" //数据接口
                ,page: true //开启分页
                ,cols: [[ //表头
                    {checkbox: true}
                    ,{field: 'customer', title: '客户名称',templet:function (d) {
                            if (d.customer !== null){
                                return '<a lay-event="show" style="cursor: pointer;color: green" title="点击查看">'+ d.customer.name+ '</a>'
                            }else{
                                return '商机已被删除'
                            }

                        }}
                    ,{field: 'num', title: '订单号',width: 160}
                    ,{field: 'stime', title: '下单日期'}
                    ,{field: 'etime', title: '交单日期'}
                    ,{field: 'total_money', title: '总金额'}
                    ,{field: 'payed_money', title: '已收款'}
                    ,{field: 'remark', title: '备注'}

                    ,{field: 'check_status', title: '审核状态',templet:function (d) {
                            if (d.check_status===1){
                                return '<span class="layui-badge" lay-event="check">待审核</span>'
                            }else if (d.check_status===2){
                                return '<span class="layui-badge layui-bg-black" lay-event="check">未通过</span>'
                            }else if (d.check_status===3){
                                return '<span class="layui-badge layui-bg-green" lay-event="check">通过</span>'
                            }
                        },width: 100}
                    ,{field: 'check_user_nickname', title: '审核人'}
                    ,{field: 'check_result', title: '审核备注'}
                    ,{field: 'owner_user_nickname', title: '下单人'}
                    ,{field: 'created_at', title: '下单时间'}
                    ,{field: 'accept_user_nickname', title: '接单人'}
                    ,{field: 'accept_time', title: '接单时间'}
                    ,{fixed: 'right', width: 100, align:'center', toolbar: '#options', title:'操作'}
                ]]
            });

            //监听工具条
            table.on('tool(dataTable)', function(obj){ //注：tool是工具条事件名，dataTable是table原始容器的属性 lay-filter="对应的值"
                var data = obj.data //获得当前行数据
                    ,layEvent = obj.event; //获得 lay-event 对应的值
                var that = this;
                var id = data.id;
                if (layEvent === 'show'){
                    layer.open({
                        type: 2,
                        title: "详情",
                        shadeClose: true,
                        area: ["90%","90%"],
                        content: '/crm/customer/'+data.customer_id+'/show',
                    })
                } else if (layEvent === 'check'){
                    @can('caiwu.order.check')
                    layer.open({
                        type: 2,
                        title: "审核",
                        shadeClose: true,
                        area: ["500px","380px"],
                        content: '/caiwu/order/check?id='+id,
                    })
                    @endcan
                } else if (layEvent === 'more'){
                    dropdown.render({
                        elem: that
                        ,show: true //外部事件触发即显示
                        ,data: [
                            @can('caiwu.order.destroy')
                            {title: '删除', id: 'del'},
                            @endcan
                            @can('caiwu.order.assignment')
                            {title: '分配', id: 'assignment'},
                            @endcan
                            @can('caiwu.order.pay')
                            {title: '收款', id: 'pay'},
                            @endcan
                            @can('caiwu.order.edit')
                            {title: '编辑', id: 'edit'},
                            @endcan
                            @can('caiwu.order.remarkList')
                            {title: '生产', id: 'remarkList'},
                            @endcan
                        ]
                        ,click: function(data, othis){
                            //根据 id 做出不同操作
                            switch (data.id) {
                                case 'del':
                                    deleteData(obj,"{{ route('caiwu.order.destroy') }}");
                                    break;
                                case 'assignment':
                                    layer.open({
                                        type: 2,
                                        title: "分配",
                                        shadeClose: true,
                                        area: ["500px","380px"],
                                        content: '/caiwu/order/assignment?order_id=' + id,
                                    })
                                    break;
                                case 'pay':
                                    layer.open({
                                        type: 2,
                                        title: "收款",
                                        shadeClose: true,
                                        area: ["600px","600px"],
                                        content: '/caiwu/order/pay?id=' + id,
                                    })
                                    break;
                                case 'edit':
                                    layer.open({
                                        type: 2,
                                        title: "编辑",
                                        shadeClose: true,
                                        area: ["80%","90%"],
                                        content: '/caiwu/order/'+id+'/edit',
                                    })
                                    break;
                                case 'remarkList':
                                    layer.open({
                                        type: 2,
                                        title: "订单生产",
                                        shadeClose: true,
                                        area: ["90%","90%"],
                                        content: '/caiwu/order/remark/list?id='+id,
                                    })
                                    break;
                                default:
                                    break
                            }
                        }
                        ,align: 'center' //右对齐弹出（v2.6.8 新增）
                        ,style: 'box-shadow: 1px 1px 10px rgb(0 0 0 / 12%);' //设置额外样式
                    });
                }
            });

            $("#addBtn").click(function () {
                layer.open({
                    type: 2,
                    title: "添加",
                    shadeClose: true,
                    area: ["80%","90%"],
                    content: "{{route("caiwu.order.create")}}",
                })
            })
        })
    </script>
@endsection
